package com.lsf.dunlian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lsf.dunlian.R;
import com.lsf.dunlian.callback.ListClickCallback;
import com.lsf.dunlian.entity.BasicMember;
import com.lsf.dunlian.entity.MemberListTitleEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MemberListTitleRecyclerViewAdapter extends RecyclerView.Adapter<MemberListTitleRecyclerViewAdapter.ViewHolder> {
    private List<MemberListTitleEntity> mDataList;
    private Context context;

    private ListClickCallback clickCallback;
    public MemberListTitleRecyclerViewAdapter(List<MemberListTitleEntity> mDataList, Context context) {
        this.mDataList = mDataList;
        this.context = context;
    }

    public void setClickCallback(ListClickCallback clickCallback) {
        this.clickCallback = clickCallback;
    }

    @NonNull
    @Override
    public MemberListTitleRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_member_title, parent, false);
        MemberListTitleRecyclerViewAdapter.ViewHolder holder = new MemberListTitleRecyclerViewAdapter.ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MemberListTitleEntity recordData = mDataList.get(position);
        holder.title.setText(recordData.title);
        if (recordData.select) {
            holder.line.setVisibility(View.VISIBLE);
            holder.title.setTextColor(context.getResources().getColor(R.color.zhuti));
        } else {
            holder.line.setVisibility(View.GONE);
            holder.title.setTextColor(context.getResources().getColor(R.color.black));
        }
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (MemberListTitleEntity entity:mDataList) {
                    entity.select=false;
                }
                recordData.select=true;
                clickCallback.click(recordData.title);
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.line)
        RelativeLayout line;
        @BindView(R.id.layout)
        LinearLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
